﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Columns;
using LPV_POS.Module;
using DevExpress.XtraGrid.Views.Base;
using LPV_POS.mh_pos.mh_danhmuc;
using System.Resources;
using System.Globalization;
using LPV_POS.thuvien_cls;

namespace LPV_POS.mh_pos
{
    public partial class mh_dmkhuyenmai : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        string str_lang = "";
        thongbao_lang clsthongbao;
        Mod_khuyenmai_hanghoa_diem khuyenmaidiem;
        xulyngaythangnam ngaythang;
        thuvien tv;
        DataTable tb;
      // string str_delete_item = "";
        DataTable tbDM_KHUYENMAI_CT_Delete = null;
        string[] s;
        object[] o;
        string makhuyenmai = "";

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_banhang6_PhuThu).Assembly);
                groupControl1.Text = rm.GetString("thongtinkhuyenmai", culture);
                label1.Text = rm.GetString("tenkhuyenmai", culture);
                label6.Text = rm.GetString("tungay", culture);
                label30.Text = rm.GetString("ghichu", culture);
                label7.Text = rm.GetString("denngay", culture);
                label9.Text = rm.GetString("apdungchocacngay", culture);
                label2.Text = rm.GetString("khohang", culture);
                label4.Text = rm.GetString("mon", culture);
                lbltongtien.Text = rm.GetString("tongdoanhthu", culture);
                label10.Text = rm.GetString("giatri", culture);
                label8.Text = rm.GetString("tugio", culture);
                label5.Text = rm.GetString("dengio", culture);
                label19.Text = rm.GetString("hinhthuc", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_sua.Text = rm.GetString("sua", culture);
                bton_xoa.Text = rm.GetString("xoa", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                bton_thoat.Text = rm.GetString("thoat", culture);
                page_1.Text = rm.GetString("khuyenmaikhachhangtuychon", culture);
                this.grv_khuyenmai.GroupPanelText = rm.GetString("danhsachkhuyenmai", culture);
                this.col_tenkhuyenmai.Caption = rm.GetString("khuyenmai", culture);
                this.col_GHI_CHU.Caption = rm.GetString("ghichu", culture);
                page_2.Text = rm.GetString("khuyenmaitatcakhachhang", culture); //"Khuyến mãi tất cả khách hàng";
                page_3.Text = rm.GetString("khuyenmaidoidiem", culture);
                check_2.Text = rm.GetString("thu_2", culture);
                check_3.Text = rm.GetString("thu_3", culture);
                check_4.Text = rm.GetString("thu_4", culture);
                check_5.Text = rm.GetString("thu_5", culture);
                check_6.Text = rm.GetString("thu_6", culture);
                check_7.Text = rm.GetString("thu_7", culture);
                check_cn.Text = rm.GetString("chunhat", culture);
                bton_themhanghoaban.Text = rm.GetString("them", culture);
                bton_xoahanghoaban.Text = rm.GetString("xoa", culture);
                bton_themhanghoatang.Text = rm.GetString("them", culture);
                bton_xoahanghoatang.Text = rm.GetString("xoa", culture);
                label21.Text = rm.GetString("soluongban", culture);
                label22.Text = rm.GetString("soluongtang", culture);
                this.col_tenhanghoaban.Caption  = rm.GetString("hanghoaban", culture);
                col_dvt_hanghoaban.Caption  = rm.GetString("dvt", culture);
                this.col_tenhanghoatang.Caption = rm.GetString("hanghoatang", culture);
                this.col_dvt_hanghoatang.Caption = rm.GetString("dvt", culture);

                grid_hanghoakm.GroupPanelText = rm.GetString("danhsachhanghoa", culture);
                this.grid_khachhangkm.GroupPanelText  = rm.GetString("danhsachnhomkh", culture);
                btnXoa_HangHoaKM.Text = rm.GetString("xoa", culture);
                btnTheVIP.Text = rm.GetString("thevip", culture);
                this.col_tenhanghoa.Caption = rm.GetString("mon", culture);
                this.col_tennhomkh.Caption = rm.GetString("nhomkhachhang", culture);
                bton_themquatang.Text = rm.GetString("them", culture);
                bton_xoaquatang.Text = rm.GetString("xoa", culture);
                bton_themhh_tangdiem.Text = rm.GetString("them", culture);
                bton_xoahh_tangdiem.Text = rm.GetString("xoa", culture);
                radio_tangqua.Text = rm.GetString("quatang", culture);
                label27.Text = rm.GetString("tenquatang", culture);
                label28.Text = rm.GetString("dvt", culture);
                radio_tangmathang.Text = rm.GetString("tangmathang", culture);
                this.col_tenquatang.Caption = rm.GetString("tenquatang", culture);
                this.col_dvtquatang.Caption = rm.GetString("dvt", culture);
                this.gridColumn2.Caption= rm.GetString("hanghoatang", culture);
                this.col_dvt_tangdiem.Caption= rm.GetString("dvt", culture);
                label24.Text = rm.GetString("sodiemdat", culture);
                label23.Text = rm.GetString("soquatang", culture);
                bton_cauhinhdiem.Text = rm.GetString("cauhinhdiem", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_them.Text = rm.GetString("them", culture);
                bton_them.Text = rm.GetString("them", culture);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }

        }

        #endregion

        // select danh sach khuyen mai
        private DataTable f_selectdmkhuyenmai()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 5 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach nhom khach khang 
        private DataTable f_selectdsnhomkhforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 1 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn nhóm khách hàng ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // select danh sach kho hang 
        private DataTable f_selectdskhohangforcom()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 2 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            DataRow myNewRow;
            myNewRow = tb.NewRow();
            myNewRow["OBJID"] = "1";
            myNewRow["OBJNAME"] = "--- Chọn kho hàng ---";
            tb.Rows.Add(myNewRow);
            //  tb.Rows.Add(n);
            tb.Columns.Add("STT", typeof(int));
            for (int i = 0; i < tb.Rows.Count; i++)
                tb.Rows[i]["STT"] = i + 1;

            //DataView dv_temp1 = tb.DefaultView;
            dv_temp.Sort = "STT DESC";
            return dv_temp.ToTable();
        }

        // select danh sach nhom khach hang theo ma khuyen mai
        private DataTable f_selectdsnhomkhtheonhomkm(string makhuyenmai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI" };
            o = new object[] { 1, 3, makhuyenmai };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach hang hoa theo ma khuyen mai
        private DataTable f_selectdshanghoatheonhomkm(string makhuyenmai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI" };
            o = new object[] { 1, 4, makhuyenmai };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach qua tang theo ma khuyen mai
        private DataTable f_selectdsquatangtheonhomkm(string makhuyenmai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI" };
            o = new object[] { 1, 12, makhuyenmai };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "OBJID DESC";
            return dv_temp.ToTable();
        }

        // select danh sach hang hoa
        private DataTable f_selectdshanghoa()
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose" };
            o = new object[] { 1, 6 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            DataView dv_temp = tb.DefaultView;
            dv_temp.Sort = "MAHHBAN DESC";
            return dv_temp.ToTable();
        }

        // insert them mot khuyen mai
        private int f_insertkhuyenmai(string tenkhuyenmai, string hinhthucthanhtoan, string makhohang, byte checktungay, string tungay, string denngay, byte thu_2, byte thu_3, byte thu_4, byte thu_5,
                                                        byte thu_6, byte thu_7, byte thu_cn, string tugio, string dengio, decimal giatri, decimal tongtien, string _GHI_CHU)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJNAME","@HINHTHUC_KM","@MAKHOHANG","@CHECK_TUNGAY" ,"@TUNGAY_str","@DENNGAY_str","THU_2","THU_3","THU_4","THU_5","THU_6","THU_7","CHUNHAT",
                                        "@TUGIO_str","@DENGIO_str", "@THAMSO_1","@THAMSO_2","@GHI_CHU"};
            o = new object[] { 2, 1, tenkhuyenmai, hinhthucthanhtoan, makhohang, checktungay, tungay, denngay,   thu_2,  thu_3,  thu_4,  thu_5,  thu_6,  thu_7,  thu_cn,
                                                    tugio, dengio, giatri, tongtien, _GHI_CHU};
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // insert them mot nhom khach hang khuyen mai
        private int f_insertkhachhangkhuyenmai(string makhuyenmai, string manhomkhachhang)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID", "@MATHE" };
            o = new object[] { 2, 2, makhuyenmai, manhomkhachhang };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // insert them hang hoa khuyen mai
        private int f_inserthhkhuyenmai(string makhuyenmai, string mahhchonkm, string mahhkhuyenmai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI", "@MAHHBAN", "@MAHHTANG" };
            o = new object[] { 2, 3, makhuyenmai, mahhchonkm, mahhkhuyenmai };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // insert mot qua tang vao khuyen mai
        private int f_inserthhkhuyenmai(string makhuyenmai, int maquatang)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI", "MAQUATANG" };
            o = new object[] { 2, 4, makhuyenmai, maquatang };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // update mot khuyen mai
        private int f_updatekhuyenmai(string makhuyenmai, string tenkhuyenmai, string mahinhthuckhuyenmai,string makhohang, byte checktungay, string tungay, string denngay, byte thu_2, byte thu_3, byte thu_4, byte thu_5, byte thu_6, byte thu_7, byte thu_cn,
                                                        string tugio, string dengio, decimal giatri, decimal tongtien, string _GHI_CHU)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI","@OBJNAME","@HINHTHUC_KM","@MAKHOHANG",
                                "@CHECK_TUNGAY" ,"@TUNGAY_str","@DENNGAY_str",
                                "THU_2","THU_3","THU_4","THU_5","THU_6","THU_7","CHUNHAT",
                                "@TUGIO_str","@DENGIO_str", "@THAMSO_1","@THAMSO_2","@GHI_CHU" };
            o = new object[] { 3, 1, makhuyenmai, tenkhuyenmai, mahinhthuckhuyenmai, makhohang, 
                                checktungay, tungay, denngay,   
                                thu_2,  thu_3,  thu_4,  thu_5,  thu_6,  thu_7,  thu_cn,
                                tugio,   dengio,   giatri, tongtien, _GHI_CHU};
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // update mot nhom khach hang khuyen mai
        private int f_updatekhachhangkhuyenmai(string makhuyenmai, string manhomkhachhang)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI", "@MATHE" };
            o = new object[] { 3, 2, makhuyenmai, manhomkhachhang };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // update them hang hoa khuyen mai
        private int f_updatehhkhuyenmai(string makhuyenmai, string mahhchonkm)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@MAKHUYENMAI", "@MAHHBAN" };
            o = new object[] { 3, 3, makhuyenmai, mahhchonkm };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        // xoa khuyen mai
        private int f_deletekhuyenmai(string makhuyenmai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 1, makhuyenmai };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        //  xoa chi tiet khuyen mai
        private int f_deletekhuyenmai_ct(string makhuyenmai)
        {
            tv = new thuvien();
            s = new string[] { "@flag", "@choose", "@OBJID" };
            o = new object[] { 4, 2, makhuyenmai };
            int ketqua = tv.SP_Update("sp_mh_dmkhuyenmai", s, o);
            return ketqua;
        }

        private void f_checkcacthu()
        {
            check_2.Checked = true;
            check_3.Checked = true;
            check_4.Checked = true;
            check_5.Checked = true;
            check_6.Checked = true;
            check_7.Checked = true;
            check_cn.Checked = true;
        }

        private void f_ucheckcacthu()
        {
            check_2.Checked = false;
            check_3.Checked = false;
            check_4.Checked = false;
            check_5.Checked = false;
            check_6.Checked = false;
            check_7.Checked = false;
            check_cn.Checked = false;
        }

        private Boolean f_un_check_thu(string dk)
        {
            Boolean ketqua = false;
            if (dk == "Checked")
                ketqua = true;
            else if (dk == "Unchecked")
                ketqua = false;
            return ketqua;
        }

        //----------------------------- The hien hinh thuc khuyen mai --------------------
        private DataTable f_showdata_comthinhthuckhuyenmai()
        {
            tv = new thuvien();
            s = new string[] {"@flag","@choose" };
            o = new object[] {1,11 };
            tb = tv.SP_SelectDataTable("sp_mh_dmkhuyenmai", s, o);
            return tb;
        }

        //-------------------------------------------------

        public mh_dmkhuyenmai()
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            tv = new thuvien();
            khuyenmaidiem = new Mod_khuyenmai_hanghoa_diem();
            tv.f_showdataoncombox(com_dvt, khuyenmaidiem.f_select_dvt(), "OBJID", "OBJNAME");
            tv.f_showdataoncombox(com_nhomkh, f_selectdsnhomkhforcom(), "OBJID", "OBJNAME");
            tv.f_showdataoncombox(com_khohang, f_selectdskhohangforcom(), "OBJID", "OBJNAME");
            tv.f_showdataoncombox(com_hinhthuckhuyenmai, f_showdata_comthinhthuckhuyenmai(),"MA_HTKM","TEN_HTKM");

        }

        private void time_dengio_Click(object sender, EventArgs e)
        {
            time_dengio.SelectedText = new cpublic().loadformso("mh_khachhang");
            
        }

        private void mh_dmkhuyenmai_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);

            grd_khuyenmai.MainView = grv_khuyenmai;
            grd_khuyenmai.DataSource = f_selectdmkhuyenmai();
            luoi_quatang.DataSource = khuyenmaidiem.f_select_khuyenmaiquatang();

            if (grv_khuyenmai.RowCount > 0)
            {
                //string makhuyenmai = grid_khuyenmai.GetRowCellValue(grid_khuyenmai.FocusedRowHandle, col_makhuyenmai).ToString() + "";
                makhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_makhuyenmai).ToString() + "";
                luoi_khachhangkm.MainView = grid_khachhangkm;
                luoi_khachhangkm.DataSource = f_selectdsnhomkhtheonhomkm(makhuyenmai);

            }
            else
            {
                time_tugio.Text = DateTime.Now.ToString("dd/MM/yyyy");
                time_dengio.Text = DateTime.Now.ToString("dd/MM/yyyy");
            }

            f_checkcacthu();
            

        }

        private void time_tugio_Click_1(object sender, EventArgs e)
        {

            time_tugio.SelectedText = new cpublic().loadformso("mh_khachhang");
        }

        private void bton_them_Click(object sender, EventArgs e)
        {
            makhuyenmai = "";
            com_nhomkh.Text = "--- Chọn nhóm khách hàng ---";
            txt_tenkhuyenmai.Text = "";
            txtGhichu.Text = "";

            f_checkcacthu();
            com_khohang.Text = "--- Chọn kho hàng ---";
            com_hanghoa.Text = "--- Chọn hàng hóa ---";
            txt_giatri.Text = "";
            time_tugio.Time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0);
            time_dengio.Time = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
            bton_luu.Enabled = true;
            bton_tenkhuyenmai.Enabled = true;
            bton_giatri.Enabled = true;
            check_tungay.Checked = true;
            date_tungay.DateTime = DateTime.Now;
            date_denngay.DateTime = DateTime.Now;
            com_hinhthuckhuyenmai.SelectedIndex = 0;

            luoi_hanghoaban.DataSource = null;
            luoi_hanghoatang.DataSource = null;
            luoi_hanghoakm.DataSource = null;
            luoi_khachhangkm.DataSource = null;
            luoi_tangdiem.DataSource = null;

            Variable.str_trangthaithemsua = "them";
            if (luoi_hanghoakm.DataSource == null)
            {
                DataTable tbHangHoa_theoNhomKM = f_selectdshanghoatheonhomkm("");

                luoi_hanghoakm.DataSource = tbHangHoa_theoNhomKM;
            }
            else
            {
                DataTable tb = (DataTable)luoi_hanghoakm.DataSource;
                if (tb != null)
                    tb.Rows.Clear();
            }
            //
            string objNAME_KHO = new cpublic().Registry_Get("frmBH_CAU_HINHcboKHO_MACDINH");
            com_khohang._SetselectedID(objNAME_KHO);
        }

        private void bton_sua_Click(object sender, EventArgs e)
        {
            bton_luu.Enabled = true;
            bton_tenkhuyenmai.Enabled = true;
            bton_giatri.Enabled = true;
            Variable.str_trangthaithemsua = "sua";
        }

        private void bton_xoa_Click(object sender, EventArgs e)
        {
            clsthongbao = new thongbao_lang();
            int ketqua;
            if (grd_khuyenmai.DefaultView.RowCount <= 0)    // neu khong co dong nao thi khong xoa duoc
                return;
            makhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_makhuyenmai).ToString();
            //Mod_messagebox mess = new Mod_messagebox();
            //mess.messagebox_pos("Thông báo", "Bạn muốn xóa?", loaithongbao.yesno.GetHashCode());
            clsthongbao.show_info(str_lang,"chatchan");
            if (Variable.thongbao == true)
            {
                ketqua = f_deletekhuyenmai(makhuyenmai);
                if (ketqua == -1)
                {
                    //Mod_messagebox mess = new Mod_messagebox();
                    //mess.messagebox_pos("Thông báo", "Xóa không thành công, kiểm tra lai khoa ngoại", loaithongbao.canhbao.GetHashCode());
                    clsthongbao.show_info(str_lang, "khongthanhcong");
                    return;
                }

                int chiso;
                // xoa mot dong trong luoi khuyen mai
                chiso = ((ColumnView)grd_khuyenmai.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)grd_khuyenmai.Views[0].DataSource;
                tam.Delete(chiso);
                if (tab_khuyenmai.SelectedTabPage != page_1)
                    tab_khuyenmai.SelectedTabPage = page_1;

                luoi_khachhangkm.MainView = grid_khachhangkm;
                luoi_khachhangkm.DataSource = null;

                //luoi_hanghoakm.MainView = grid_hanghoakm;
                luoi_hanghoakm.DataSource = null;

            }
        }

        private void grid_khuyenmai_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (grv_khuyenmai.RowCount <= 0)
                return;
            Variable.str_trangthaithemsua = "";
            GridView grid = (GridView)sender;
            int chiso = 0;
            if (e != null)
                chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grv_khuyenmai.FocusedColumn;
            txt_tenkhuyenmai.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tenkhuyenmai) + "";
            txtGhichu.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_GHI_CHU) + "";

            //date_tungay.Text = grid_khuyenmai.GetRowCellDisplayText(chiso, col_tungay) + "";
            if (grv_khuyenmai.GetRowCellValue(chiso, col_tungay) + "" != "")
                date_tungay.DateTime = (DateTime)grv_khuyenmai.GetRowCellValue(chiso, col_tungay);
            else
                date_tungay.Text = "";


            // date_denngay.Text = grid_khuyenmai.GetRowCellDisplayText(chiso, col_denngay) + "";
            if (grv_khuyenmai.GetRowCellValue(chiso, col_denngay) + "" != "")
                date_denngay.DateTime = (DateTime)grv_khuyenmai.GetRowCellValue(chiso, col_denngay);
            else
                date_denngay.Text = "";

            string thu = "";
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_thu2) + "";
            check_2.Checked = f_un_check_thu(thu);
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_thu3) + "";
            check_3.Checked = f_un_check_thu(thu);
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_thu4) + "";
            check_4.Checked = f_un_check_thu(thu);
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_thu5) + "";
            check_5.Checked = f_un_check_thu(thu);
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_thu6) + "";
            check_6.Checked = f_un_check_thu(thu);
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_thu7) + "";
            check_7.Checked = f_un_check_thu(thu);
            thu = grv_khuyenmai.GetRowCellDisplayText(chiso, col_chunhat) + "";
            check_cn.Checked = f_un_check_thu(thu);

            
            com_khohang.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tenkhohang) + "";
            
            //time_tugio.Text = grid_khuyenmai.GetRowCellDisplayText(chiso, col_tugio) + "";
            //time_dengio.Text = grid_khuyenmai.GetRowCellDisplayText(chiso, col_dengio) + "";

            string s1 = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tugio) + "";
            string s2 = grv_khuyenmai.GetRowCellDisplayText(chiso, col_dengio) + "";
            //time_dengio.Text = s2;\
            if(s1!="")
                time_tugio.Time = DateTime.Parse(s1);
            if (s2 != "")
                time_dengio.Time = DateTime.Parse(s2);

            string makm = grv_khuyenmai.GetRowCellDisplayText(chiso, col_makhuyenmai) + "";

            com_hinhthuckhuyenmai._SetselectedID(grv_khuyenmai.GetRowCellDisplayText(chiso, col_hinhthuckhuyenmai) + "");
            //-------------------------------------------------
            DataTable tbnhomkh_theonhomkm = f_selectdsnhomkhtheonhomkm(makm);
            luoi_khachhangkm.DataSource = tbnhomkh_theonhomkm;
            DataTable tb_hhkhm;
            string sHinhThuc = com_hinhthuckhuyenmai._GetselectedID();
            switch (sHinhThuc)
            {
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "14":
                case "16":
                case "17":
                case "18":
                case "19":
                    #region "   ............................"
                    luoi_hanghoakm.DataSource = null;
                    txt_giatri.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";
                    txt_tongtiengiam.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tongtien) + "";

                    //DataTable tbnhomkh_theonhomkm = f_selectdsnhomkhtheonhomkm(makm);
                    //luoi_khachhangkm.DataSource = tbnhomkh_theonhomkm;

                    DataTable tbHangHoa_theoNhomKM = f_selectdshanghoatheonhomkm(makm);
                    //luoi_hanghoakm.MainView = grid_hanghoakm;
                    luoi_hanghoakm.DataSource = tbHangHoa_theoNhomKM;
                    //luoi_hanghoakm.MainView = grid_hanghoakm;

                    if (tbDM_KHUYENMAI_CT_Delete == null)
                        tbDM_KHUYENMAI_CT_Delete = tbHangHoa_theoNhomKM.Clone();

                    bton_tenkhuyenmai.Enabled = false;
                    bton_giatri.Enabled = false;
                    #endregion
                    break;
                case "8":      //
                case "13":      //
                    #region "   mua x mon A tang y mon B"
                    tb_hhkhm = null;
                    luoi_hanghoakm.DataSource = null;
                    tb_hhkhm = f_selectdshanghoatheonhomkm(makm);
                    DataTable tbHHBan = tb_hhkhm.Copy();
                    for (int ihhban = 0; ihhban < tbHHBan.Rows.Count; ihhban++)
                    {
                        if (tbHHBan.Rows[ihhban]["MAHHBAN"] + "" == "")
                            tbHHBan.Rows.RemoveAt(ihhban--);
                    }
                    luoi_hanghoaban.DataSource = tbHHBan;
                    luoi_hanghoatang.DataSource = tb_hhkhm.Copy();
                    if (sHinhThuc=="13")
                        txt_tongtiengiam.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";
                    else
                        txt_soluongban.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";

                    txt_soluongtang.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tongtien) + "";
                    lbel_donvitang.Text = grid_hanghoatang.GetRowCellDisplayText(grid_hanghoatang.FocusedRowHandle, col_dvt_hanghoatang);
                    tab_khuyenmai.SelectedTabPage = page_2; 
                    //
                    
                    #endregion
                    break;
                case "9":   // 
                    #region "   mua x mon A giam y tien"
                    tb_hhkhm = null;
                    luoi_hanghoakm.DataSource = null;
                    tb_hhkhm = f_selectdshanghoatheonhomkm(makm);
                    luoi_hanghoaban.DataSource = tb_hhkhm;
                    luoi_hanghoatang.DataSource = null;

                    txt_soluongtang.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tongtien) + "";
                    txt_soluongban.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";
                    lbel_donvitang.Text = "đồng";
                    tab_khuyenmai.SelectedTabPage = page_2;
                    #endregion
                    break;
                case "10":     //  
                    #region "   mua x mon A giam y% tien"
                    tb_hhkhm = null;
                    luoi_hanghoakm.DataSource = null;
                    tb_hhkhm = f_selectdshanghoatheonhomkm(makm);
                    luoi_hanghoaban.DataSource = tb_hhkhm;
                    luoi_hanghoatang.DataSource = null;

                    txt_soluongtang.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tongtien) + "";
                    txt_soluongban.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";
                    lbel_donvitang.Text = "%";
                    tab_khuyenmai.SelectedTabPage = page_2;
                    #endregion
                    break;
                case "11":         // 
                    #region "   dat x diem tang y tien"
                    luoi_hanghoakm.DataSource = null;
                    //tbnhomkh_theonhomkm = f_selectdsnhomkhtheonhomkm(makm);
                    //luoi_khachhangkm.DataSource = tbnhomkh_theonhomkm;
                    txt_sodiemdat.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";
                    txt_soquatang.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tongtien) + "";
                    luoi_tangdiem.DataSource = null;
                    //luoi_quatang.DataSource = null;
                    radio_tangqua.Checked = false;
                    tab_khuyenmai.SelectedTabPage = page_3;
                    #endregion
                    break;
                case "12":
                case "15":
                    #region "   dat x diem tang y qua"
                    tb_hhkhm = null;
                    tb_hhkhm = f_selectdsquatangtheonhomkm(makm);
                    luoi_tangdiem.DataSource = tb_hhkhm;

                    luoi_hanghoakm.DataSource = null;
                    // tbnhomkh_theonhomkm = f_selectdsnhomkhtheonhomkm(makm);
                    //luoi_khachhangkm.DataSource = tbnhomkh_theonhomkm;
                    txt_sodiemdat.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_giatri) + "";
                    txt_soquatang.Text = grv_khuyenmai.GetRowCellDisplayText(chiso, col_tongtien) + "";
                    if(grid_tangdiem.RowCount>0)
                        label25.Text = grid_tangdiem.GetRowCellDisplayText(grid_tangdiem.FocusedRowHandle, col_dvt_tangdiem);

                    string phanloai = "";
                    if (tb_hhkhm.Rows.Count > 0)
                        phanloai = tb_hhkhm.Rows[0]["PHANLOAI"] + "";
                    if (phanloai == "1")
                        radio_tangqua.Checked = true;
                    else
                        if (phanloai == "2")
                            radio_tangmathang.Checked = true;
                    
                    tab_khuyenmai.SelectedTabPage = page_3;
                    #endregion
                    break;
            }    
        }

        private void grid_khachhangkm_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            GridView grid = (GridView)sender;
            int chiso = e.FocusedRowHandle;
            string _chiso = Convert.ToString(chiso);
            GridColumn col = grid_khachhangkm.FocusedColumn;
            com_nhomkh.Text = grid_khachhangkm.GetRowCellDisplayText(chiso, col_tennhomkh) + "";
        }

        private void grid_hanghoakm_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            //luoi_hanghoakm.MainView = grid_hanghoakm;
            //GridView grid = (GridView)sender;
            //int chiso = e.FocusedRowHandle;
            //string _chiso = Convert.ToString(chiso);
            //GridColumn col = grid_hanghoakm.FocusedColumn;
            //com_hanghoa.Text = grid_hanghoakm.GetRowCellDisplayText(chiso, col_tenhanghoa) + "";
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            if (Variable.str_trangthaithemsua == "")
            {
                MessageBox_LPV.Show("Bạn phải chọn THÊM hoặc SỬA");
                return;
            }

            //com_nhomkh.Text == "--- Chọn nhóm khách hàng ---" || 
            if (txt_tenkhuyenmai.Text == "" || com_khohang.Text == "--- Chọn kho hàng ---")
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Nhập đầy đủ thông tin?", loaithongbao.canhbao.GetHashCode());
                return;
            }

            if (lbel_donvi.Text == "%")
            {
                int itien = 0;

                int.TryParse(txt_giatri.Text, out itien);

                if (itien < 0 || itien > 100)
                {
                    MessageBox_LPV.Show("Giá trị phần trăm khuyến mãi phải \n từ 0 đến 100");
                    return;
                }
            }

            int chiso = -1;
            // string mahanghoa = "";
            ngaythang = new xulyngaythangnam();
            string tenkhuyenmai = txt_tenkhuyenmai.Text;
            string nhomkhachhang = com_nhomkh._GetselectedID();
            string makhohang = com_khohang._GetselectedID();
            //string hanghoa = com_hanghoa._GetselectedID();
            decimal giatri = 0;
            decimal tongtien = 0;
            string mahinhthuckhuyenmai = com_hinhthuckhuyenmai._GetselectedID();
            switch (mahinhthuckhuyenmai)
            {
                case "2":
                case "3":
                case "4":
                case "5":
                case "6":
                case "7":
                case "14":
                case "16":
                case "17":
                case "18":
                case "19":
                    if (luoi_hanghoakm.DataSource != null)
                    {
                        DataTable tb = (DataTable)luoi_hanghoakm.DataSource;
                        if (tb.Rows.Count == 0)
                        {
                            Mod_messagebox mess = new Mod_messagebox();
                            mess.messagebox_pos("Thông báo", "Chưa nhập thông tin hàng hóa!", loaithongbao.canhbao.GetHashCode());
                            //return;
                        }
                    }

                    if (txt_giatri.Text == "")
                    {
                        Mod_messagebox mess = new Mod_messagebox();
                        mess.messagebox_pos("Thông báo", "Nhập giá trị", loaithongbao.canhbao.GetHashCode());
                        return;
                    }
                    Decimal.TryParse(txt_giatri.Text, out giatri);
                    if (txt_tongtiengiam.Text == "")
                        txt_tongtiengiam.Text = "0";
                    Decimal.TryParse(txt_tongtiengiam.Text, out tongtien);
                    break;

                case "8":
                    if (grid_hanghoaban.RowCount != grid_hanghoatang.RowCount)
                    {
                        Mod_messagebox mess = new Mod_messagebox();
                        mess.messagebox_pos("Thông báo", "Số món bán và tặng không bằng nhau", loaithongbao.canhbao.GetHashCode());
                        //return;
                    }
                    Decimal.TryParse(txt_soluongban.Text, out giatri);
                    Decimal.TryParse(txt_soluongtang.Text, out tongtien);
                    break;

                case "9":
                case "10":
                    Decimal.TryParse(txt_soluongban.Text, out giatri);
                    Decimal.TryParse(txt_soluongtang.Text, out tongtien);
                    break;

                case "11":
                case "12":
                case "15":
                    Decimal.TryParse(txt_sodiemdat.Text, out giatri);
                    Decimal.TryParse(txt_soquatang.Text, out tongtien);
                    break;
                case "13":
                    break;
            }

            #region "   "
            byte thu_2 = 0;
            byte thu_3 = 0;
            byte thu_4 = 0;
            byte thu_5 = 0;
            byte thu_6 = 0;
            byte thu_7 = 0;
            byte thu_cn = 0;
            if (check_2.Checked == true)
                thu_2 = 1;
            else
                thu_2 = 0;

            if (check_3.Checked == true)
                thu_3 = 1;
            else
                thu_3 = 0;

            if (check_4.Checked == true)
                thu_4 = 1;
            else
                thu_4 = 0;

            if (check_5.Checked == true)
                thu_5 = 1;
            else
                thu_5 = 0;

            if (check_6.Checked == true)
                thu_6 = 1;
            else
                thu_6 = 0;

            if (check_7.Checked == true)
                thu_7 = 1;
            else
                thu_7 = 0;

            if (check_cn.Checked == true)
                thu_cn = 1;
            else
                thu_cn = 0;
            byte checktungay = 0;
            if (check_tungay.Checked == true)
                checktungay = 1;
            else
                checktungay = 0;

            string tungay;
            if (date_tungay.Text != "")
                tungay = ngaythang.chuyenngay(date_tungay.Text);
            else
                tungay = date_tungay.Text;

            string denngay;
            if (date_denngay.Text != "")
                denngay = ngaythang.chuyenngay(date_denngay.Text);
            else
                denngay = date_denngay.Text;
            if (check_tungay.Checked)
            {
                if (int.Parse(tungay) > int.Parse(denngay))
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Từ ngày phải nhỏ hơn đến ngày", loaithongbao.canhbao.GetHashCode());
                    return;
                }
            }
            #endregion

            string tugio = time_tugio.Text;
            string dengio = time_dengio.Text;


            if (mahinhthuckhuyenmai == "1")
            {
                Mod_messagebox mess = new Mod_messagebox();
                mess.messagebox_pos("Thông báo", "Phải chọn hình thức thanh toán", loaithongbao.canhbao.GetHashCode());
                return;
            }

            int ketqua;
            //string makhuyenmai = "";
            if (grv_khuyenmai.RowCount > 0)
                makhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_makhuyenmai).ToString() + "";
            thuvien othuvien = new thuvien();

            #region "   Xoa_HangHoa"
            if (tbDM_KHUYENMAI_CT_Delete != null)
            {
                foreach (DataRow drDel in tbDM_KHUYENMAI_CT_Delete.Rows)
                {
                    string maHangHoa = drDel["MAHHBAN"] + "";
                    string sSQL = "DELETE FROM DM_KHUYENMAI_CT WHERE MAKM = '" + makhuyenmai + "' AND MAHHBAN = '" + maHangHoa + "'";

                    othuvien.SP_Update("SP_EXECUTESQL", new string[] { "@sSQL" }, new string[] { sSQL });
                }
                tbDM_KHUYENMAI_CT_Delete.Rows.Clear();
            }
            #endregion

            // insert them mot khuyen mai
            if (Variable.str_trangthaithemsua == "them")
            {
                if (mahinhthuckhuyenmai == "13")
                {
                    if (txt_tongtiengiam.Text == "")
                        txt_tongtiengiam.Text = "0";

                    Decimal.TryParse(txt_tongtiengiam.Text, out giatri);
                    Decimal.TryParse(txt_soluongtang.Text, out tongtien);
                }

                ketqua = f_insertkhuyenmai(tenkhuyenmai, mahinhthuckhuyenmai, makhohang, checktungay, tungay, denngay, thu_2, thu_3, thu_4, thu_5, thu_6, thu_7, thu_cn,
                                                         tugio, dengio, giatri, tongtien,txtGhichu.Text.Trim());
                DataTable tbKhuyenmai = f_selectdmkhuyenmai();
                makhuyenmai = tbKhuyenmai.Rows[0]["OBJID"] + "";

                if (ketqua == -1)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Thêm không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                else      // them vào nhom khách hàng khuyen mai và hàng hoa khuyến mãi
                {
                    #region "   switch (mahinhthuckhuyenmai)"
                    switch (mahinhthuckhuyenmai)
                    {
                        case "2":
                        case "3":
                        case "4":
                        case "5":
                        case "6":
                        case "7":
                        case "14":
                            #region "   "
                            //ketqua = f_insertkhachhangkhuyenmai(makhuyenmai, com_nhomkh._GetselectedID());  // insert nhom khach hang khuyen mai
                            luoi_khachhangkm.DataSource = null;
                            luoi_khachhangkm.DataSource = f_selectdsnhomkhtheonhomkm(makhuyenmai);

                            DataTable tbHangHoa = (DataTable)luoi_hanghoakm.DataSource;
                            if (tbHangHoa != null)
                                for (int i = 0; i < tbHangHoa.Rows.Count; i++)
                                    ketqua = f_inserthhkhuyenmai(makhuyenmai, tbHangHoa.Rows[i]["MAHHBAN"] + "", null);
                            if (ketqua > -1)
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", "Thêm khuyến mãi thành công", loaithongbao.canhbao.GetHashCode());

                            }
                            else
                                return;
                            break;
                            #endregion
                        case "8":
                            #region "   "
                            DataTable tb_ban = (DataTable)luoi_hanghoaban.DataSource;
                            if (tb_ban != null)
                                if (tb_ban.Rows.Count == 0)
                                    tb_ban = null;
                            DataTable tb_tang = (DataTable)luoi_hanghoatang.DataSource;
                            if (tb_tang != null)
                                if (tb_tang.Rows.Count == 0)
                                    tb_tang = null;
                            if (tb_ban != null && tb_tang != null)
                                for (int i = 0; i < tb_ban.Rows.Count; i++)
                                    f_inserthhkhuyenmai(makhuyenmai, tb_ban.Rows[i]["MAHHBAN"] + "", tb_tang.Rows[i]["MAHHBAN"] + "");

                            else if (tb_ban == null && tb_tang != null)
                                for (int i = 0; i < tb_tang.Rows.Count; i++)
                                    f_inserthhkhuyenmai(makhuyenmai, null, tb_tang.Rows[i]["MAHHBAN"] + "");

                            if (ketqua > -1)
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", "Thêm khuyến mãi thành công", loaithongbao.canhbao.GetHashCode());

                            }
                            else
                                return;
                            break;
                            #endregion
                        case "9":
                        case "10":

                            DataTable tb_ban_9 = (DataTable)luoi_hanghoaban.DataSource;

                            for (int i = 0; i < tb_ban_9.Rows.Count; i++)
                            {
                                f_inserthhkhuyenmai(makhuyenmai, tb_ban_9.Rows[i]["MAHHBAN"] + "", null);
                            }
                            if (ketqua > -1)
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", "Thêm khuyến mãi thành công", loaithongbao.canhbao.GetHashCode());

                            }
                            else
                                return;
                            break;

                        case "11":
                        case "16":
                        case "17":
                        case "18":
                        case "19":
                            new Mod_messagebox().messagebox_pos("Thông báo", "Thêm khuyến mãi thành công", loaithongbao.canhbao.GetHashCode());
                            break;

                        case "12":
                        case "15":
                            if (grid_tangdiem.RowCount < 0)
                                return;
                            if (radio_tangqua.Checked)    // tang qua
                            {
                                int mahhtang = int.Parse(grid_tangdiem.GetRowCellValue(0, col_mahhtang).ToString() + "");
                                ketqua = f_inserthhkhuyenmai(makhuyenmai, mahhtang);
                            }
                            else       // tang mat hang
                            {
                                string mahhtang = grid_tangdiem.GetRowCellValue(0, col_mahhtang).ToString() + "";
                                ketqua = f_inserthhkhuyenmai(makhuyenmai, null, mahhtang);
                            }

                            if (ketqua > -1)
                            {
                                Mod_messagebox mess = new Mod_messagebox();
                                mess.messagebox_pos("Thông báo", "Thêm khuyến mãi thành công", loaithongbao.canhbao.GetHashCode());

                            }
                            else
                                return;

                            break;
                    }
                    #endregion
                }

                grd_khuyenmai.DataSource = null;
                grd_khuyenmai.DataSource = tbKhuyenmai;

            }
            else if (Variable.str_trangthaithemsua == "sua")
            {
                if (mahinhthuckhuyenmai == "13")
                {
                    if (txt_tongtiengiam.Text == "")
                        txt_tongtiengiam.Text = "0";

                    Decimal.TryParse(txt_tongtiengiam.Text, out giatri);
                    Decimal.TryParse(txt_soluongtang.Text, out tongtien);
                }

                chiso = ((ColumnView)grd_khuyenmai.Views[0]).FocusedRowHandle;

                makhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_makhuyenmai) + "";

                // update luoi khuyen mai
                ketqua = f_updatekhuyenmai(makhuyenmai, tenkhuyenmai, mahinhthuckhuyenmai, makhohang, checktungay, tungay, denngay, thu_2, thu_3, thu_4, thu_5, thu_6, thu_7, thu_cn,
                                                         tugio, dengio, giatri, tongtien, txtGhichu.Text.Trim());

                if (ketqua == -1)
                {
                    Mod_messagebox mess = new Mod_messagebox();
                    mess.messagebox_pos("Thông báo", "Chỉnh sửa không thành công", loaithongbao.canhbao.GetHashCode());
                    return;
                }
                else // sua khuyen mai chi tiet
                {
                    #region "   switch (mahinhthuckhuyenmai)"
                    switch (mahinhthuckhuyenmai)
                    {
                        case "2":
                        case "3":
                        case "4":
                        case "5":
                        case "6":
                        case "7":
                        case "14":
                            //  ketqua = f_updatekhachhangkhuyenmai(makhuyenmai, com_nhomkh._GetselectedID());  // update nhom khach hang khuyen mai
                            luoi_khachhangkm.DataSource = null;
                            luoi_khachhangkm.DataSource = f_selectdsnhomkhtheonhomkm(makhuyenmai);

                            DataTable tbHangHoa = (DataTable)luoi_hanghoakm.DataSource;
                            if (tbHangHoa != null)
                                for (int i = 0; i < tbHangHoa.Rows.Count; i++)
                                    ketqua = f_inserthhkhuyenmai(makhuyenmai, tbHangHoa.Rows[i]["MAHHBAN"] + "", null);


                            break;

                        case "8":
                        case "13":
                            f_deletekhuyenmai_ct(makhuyenmai);
                            DataTable tb_ban = (DataTable)luoi_hanghoaban.DataSource;
                            DataTable tb_tang = (DataTable)luoi_hanghoatang.DataSource;

                            if (tb_ban != null && tb_tang != null)
                            {
                                int idem = tb_ban.Rows.Count;
                                
                                if (tb_ban.Rows.Count < tb_tang.Rows.Count)
                                    idem = tb_tang.Rows.Count;

                                for (int i = 0; i < idem; i++)
                                {
                                    if (tb_ban.Rows.Count > i && tb_tang.Rows.Count > i)
                                        f_inserthhkhuyenmai(makhuyenmai, tb_ban.Rows[i]["MAHHBAN"] + "", tb_tang.Rows[i]["MAHHTANG"] + "");
                                    else if (tb_ban.Rows.Count > i)
                                        f_inserthhkhuyenmai(makhuyenmai, tb_ban.Rows[i]["MAHHBAN"] + "", null);
                                    else if (tb_tang.Rows.Count > i)
                                        f_inserthhkhuyenmai(makhuyenmai, null, tb_tang.Rows[i]["MAHHTANG"] + "");
                                }
                            }
                            else if (tb_tang != null)
                                for (int i = 0; i < tb_tang.Rows.Count; i++)
                                {
                                    if (tb_tang.Columns.Contains("MAHHTANG"))
                                        f_inserthhkhuyenmai(makhuyenmai, null, tb_tang.Rows[i]["MAHHTANG"] + "");
                                    else if (tb_tang.Columns.Contains("MAHHBAN"))
                                        f_inserthhkhuyenmai(makhuyenmai, null, tb_tang.Rows[i]["MAHHBAN"] + "");
                                }
                            break;

                        case "9":
                        case "10":
                            f_deletekhuyenmai_ct(makhuyenmai);
                            tb_ban = (DataTable)luoi_hanghoaban.DataSource;
                            for (int i = 0; i < tb_ban.Rows.Count; i++)
                            {
                                f_inserthhkhuyenmai(makhuyenmai, tb_ban.Rows[i]["MAHHBAN"] + "", null);
                            }

                            break;

                        case "11":

                            break;
                        case "12":
                        case "15":
                            f_deletekhuyenmai_ct(makhuyenmai);
                            if (radio_tangqua.Checked)    // tang qua
                            {
                                DataRow drHHTang = grid_tangdiem.GetDataRow(grid_tangdiem.FocusedRowHandle);

                                int mahhtang = int.Parse("0" + grid_tangdiem.GetRowCellValue(0, col_mahhtang));//9999
                                if (drHHTang == null)
                                {
                                    MessageBox.Show("Chọn hàng hóa tặng");
                                    return;
                                }
                                if (mahhtang == 0)
                                    int.TryParse(drHHTang["maquatang"] + "", out mahhtang);
                                ketqua = f_inserthhkhuyenmai(makhuyenmai, mahhtang);
                            }
                            else       // tang mat hang
                            {
                                if (grid_tangdiem.RowCount > 0)
                                {
                                    string mahhtang = grid_tangdiem.GetRowCellValue(0, col_mahhtang).ToString() + "";
                                    ketqua = f_inserthhkhuyenmai(makhuyenmai, null, mahhtang);
                                }
                                else
                                {
                                    MessageBox_LPV.Show("Nhập hàng hóa(quà) tặng");
                                    return;
                                }
                            }

                            break;
                       
                    }
                    #endregion
                }

                grd_khuyenmai.DataSource = null;
                grd_khuyenmai.DataSource = f_selectdmkhuyenmai();
                if (chiso != -1)
                {
                    grv_khuyenmai.SelectRow(chiso);
                    grv_khuyenmai.FocusedRowHandle = chiso;
                }
                new Mod_messagebox().messagebox_pos("Thông báo", "Cập nhật thành công", 2);
            }
            //luoi_khuyenmai.DataSource = null;
            //luoi_khuyenmai.DataSource = f_selectdmkhuyenmai();

        }
        DataTable tbHH_KM;
        private void com_hanghoa_SelectedIndexChanged(object sender, EventArgs e)
        {

            // string tuychon = com_hanghoa.Text;
            int tuychon = com_hanghoa.SelectedIndex;
            switch (tuychon)
            {
                case 0:   //"--- Chọn hàng hóa ---":
                    {
                        return;
                    }
                case 1:     //"Tất cả hàng hóa":
                    {
                        luoi_hanghoakm.MainView = grid_hanghoakm;
                        luoi_hanghoakm.DataSource = f_selectdshanghoa();
                        com_hanghoa.SelectedIndex = 0;
                        return;
                    }
                case 2:      //"Nhóm chủ":
                    {
                        mh_khuyenmaihanghoa frm_khuyenmaihanghoa = new mh_khuyenmaihanghoa("Nhóm chủ");
                        frm_khuyenmaihanghoa.ShowDialog();
                        //com_hanghoa.SelectedIndex = 0;
                        if (frm_khuyenmaihanghoa.DialogResult != DialogResult.OK)
                            return;
                        if (frm_khuyenmaihanghoa.tbHH_KM != null)
                            tbHH_KM = frm_khuyenmaihanghoa.tbHH_KM.Copy();
                        break;
                    }
                case 3:      //"Nhóm hàng":
                    {
                        mh_khuyenmaihanghoa frm_khuyenmaihanghoa = new mh_khuyenmaihanghoa("Nhóm hàng");
                        frm_khuyenmaihanghoa.ShowDialog();
                        //com_hanghoa.SelectedIndex = 0;
                        if (frm_khuyenmaihanghoa.DialogResult != DialogResult.OK)
                            return;
                        if (frm_khuyenmaihanghoa.tbHH_KM != null)
                            tbHH_KM = frm_khuyenmaihanghoa.tbHH_KM.Copy();
                        break;
                    }
                case 4:    //"Hàng hóa":
                    {
                        mh_khuyenmaihanghoa frm_khuyenmaihanghoa = new mh_khuyenmaihanghoa("Hàng hóa");
                        frm_khuyenmaihanghoa.ShowDialog();
                        //com_hanghoa.SelectedIndex = 0;
                        if (frm_khuyenmaihanghoa.DialogResult != DialogResult.OK)
                            return;
                        if (frm_khuyenmaihanghoa.tbHH_KM != null)
                            tbHH_KM = frm_khuyenmaihanghoa.tbHH_KM.Copy();
                        break;
                    }
            }

            // chuong
             //string makhuyenmai ="";
            if(grv_khuyenmai.RowCount> 0)
                makhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_makhuyenmai).ToString() + "";

            if (luoi_hanghoakm.DataSource == null)
            {
                if (tbHH_KM != null)
                {
                    DataTable tbTMP = tbHH_KM.Copy();
                    tbTMP.Clear();
                    luoi_hanghoakm.DataSource = tbTMP.Copy();
                }
            }
            //if (luoi_hanghoakm.DataSource != null)
            {
                DataTable tbhanghoakm = (DataTable)luoi_hanghoakm.DataSource;
                if (tbHH_KM!=null)
                    foreach (DataRow drTMP in tbHH_KM.Rows)
                    {
                        if (tbhanghoakm.Columns["MAHHBAN"] != null)
                        {
                                DataRow dr = tbhanghoakm.NewRow();
                                //dr["MAKM"] = makhuyenmai;
                                dr["MAHHBAN"] = drTMP["MAHHBAN"];
                                dr["TENHHKM"] = drTMP["TENHHKM"];
                                tbhanghoakm.Rows.Add(dr);                             
                        }
                     
                    }
                //luoi_hanghoakm.MainView = grid_hanghoakm;
                //luoi_hanghoakm.DataSource = tbhanghoakm;
            }
            //else
            //{
            //    //luoi_hanghoakm.MainView = grid_hanghoa;
            //    luoi_hanghoakm.DataSource = tbHH_KM.Copy();
            //}
            return;
        }

        private void bton_thoat_Click(object sender, EventArgs e)
        {
           
            this.Close();
           
        }

        private void bton_tenkhuyenmai_Click(object sender, EventArgs e)
        {
            txt_tenkhuyenmai.Text = new cpublic().loadformchu_point(300, 200);
        }

        private void bton_giatri_Click(object sender, EventArgs e)
        {
            txt_giatri.Text = new cpublic().loadformso_point(500, 250);
        }

        private void btnTheVIP_Click(object sender, EventArgs e)
        {
            try
            {
                if (grv_khuyenmai.FocusedRowHandle < 0)
                {
                    MessageBox_LPV.Show("Chọn khuyến mãi trước khi chọn thẻ.");
                    return;
                }
                //string 
                    makhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_makhuyenmai).ToString() + "";
                string Tenkhuyenmai = grv_khuyenmai.GetRowCellValue(grv_khuyenmai.FocusedRowHandle, col_tenkhuyenmai).ToString() + "";
                frmDM_THE ofrmDM_THE = new frmDM_THE(makhuyenmai, Tenkhuyenmai);
                ofrmDM_THE.ShowDialog();
                this.grid_khuyenmai_FocusedRowChanged(null, null);
                //if (ofrmDM_THE.DialogResult == DialogResult.OK)
                //{ //9999                
                //    luoi_khachhangkm.MainView = grid_khachhangkm;
                //    luoi_khachhangkm.DataSource = f_selectdsnhomkhtheonhomkm(makhuyenmai);
                //}
            }
            catch (Exception ex)
            {
                string s = ex.Message;
                new mh_thongbao("Thông báo\n" + s, 2).ShowDialog();
            }
        }

        private void check_tungay_CheckedChanged(object sender, EventArgs e)
        {
            if (check_tungay.Checked == true)
            {
                date_tungay.DateTime = DateTime.Now;
                date_denngay.DateTime = DateTime.Now;
            }
            else
            {
                date_tungay.Text = "";
                date_denngay.Text = "";
            }
        }

        private void grid_khachhangkm_DataSourceChanged(object sender, EventArgs e)
        {

            GridColumn col = grid_khachhangkm.FocusedColumn;
            if (grid_khachhangkm.RowCount>0)
            com_nhomkh.Text = grid_khachhangkm.GetRowCellValue(grid_khachhangkm.FocusedRowHandle, col_tennhomkh).ToString();
        }

        private void btnXoa_HangHoaKM_Click(object sender, EventArgs e)
        {
            if (grid_hanghoakm.FocusedRowHandle >= 0)
            {
                grid_hanghoakm.FocusedRowHandle = grid_hanghoakm.FocusedRowHandle;
                DataRow drHangHoa = grid_hanghoakm.GetDataRow(grid_hanghoakm.FocusedRowHandle);
                string maHangHoa = drHangHoa["MAHHBAN"] + "";
                this.Xoa_HangHoa_KhuyenMai(maHangHoa);
                if (luoi_hanghoakm.DataSource != null)
                {
                    DataTable tbTMP = (DataTable)luoi_hanghoakm.DataSource;

                    tbTMP.Rows.Remove(drHangHoa);
                }
            }
            else if (grid_hanghoakm.RowCount > 0)
            {
                new mh_thongbao("Chọn hàng hóa cần xóa", 2).ShowDialog();
            }
        }
        
        void Xoa_HangHoa_KhuyenMai(string maHangHoa)
        {

            if (tbDM_KHUYENMAI_CT_Delete != null)
            {
                DataRow dr = tbDM_KHUYENMAI_CT_Delete.NewRow();
                dr["MAHHBAN"] = maHangHoa;
                tbDM_KHUYENMAI_CT_Delete.Rows.Add(dr);
            }
        }

        private void label18_Click(object sender, EventArgs e)
        {
            date_tungay.ShowPopup();
        }

        private void lbel_calendar_Click(object sender, EventArgs e)
        {
            date_denngay.ShowPopup();
        }

        private void com_hinhthuckhuyenmai_SelectedIndexChanged(object sender, EventArgs e)
        {
            //------------------- Begin hinh thuc khuyen mai moi -----------------------------------
            
           // lbltongtien.Text = "Tổng tiền";
            // 123456
            switch (str_lang)
            {
                case "vi-VN":
                    lbltongtien.Text = "Tổng tiền";
                    break;
                case "en-US":
                    lbltongtien.Text = "Total";
                    break;
                
            }
            switch (com_hinhthuckhuyenmai._GetselectedID())
            {
                case "2"://Giảm tiền trên bill
                case "3"://Giảm % trên bill
                case "4"://Giảm tiền món rẻ nhất
                case "5"://Giảm % món rẻ nhất
                case "6"://Giảm tiền món cuối cùng
                case "7"://Giảm % món cuối cùng
                    switch (str_lang)
                    {
                        case "vi-VN":
                            lbel_donvi.Text = "đồng";
                            break;
                        case "en-US":
                            lbel_donvi.Text = "VND";
                            break;
                    }

                    //lbel_donvi.Text = "đồng";
                    break;
                case "8"://Mua x món A tặng y món B
                    #region "   "
                    switch (str_lang)
                    {
                        case "vi-VN":
                            label22.Text = "Số lượng tặng";
                            break;
                        case "en-US":
                            label22.Text = "Number for giving";
                            break;
                    }

                    bton_themhanghoatang.Enabled = true;
                    bton_xoahanghoatang.Enabled = true;
                    tab_khuyenmai.SelectedTabPage = page_2;
                    //label22.Text = "Số lượng tặng";
                    lbel_donvitang.Text = lbel_donviban.Text;
                    break;
                    #endregion
                case "9":
                    #region "   Mua x món A giảm y tiền"
                    switch (str_lang)
                    {
                        case "vi-VN":
                            label22.Text = "Số tiền tặng";
                            lbel_donvitang.Text = "đồng";
                            break;
                        case "en-US":
                            label22.Text = "Money give";
                            lbel_donvitang.Text = "VND";
                            break;
                    }
                  //  lbel_donvitang.Text = "đồng";
                    bton_themhanghoatang.Enabled = false;
                    bton_xoahanghoatang.Enabled = false;
                    tab_khuyenmai.SelectedTabPage = page_2;
                    //label22.Text = "Số tiền tặng";
                    break;
                    #endregion
                case "10":
                    #region "   Mua x món A giảm y% tiền"
                    lbel_donvitang.Text = "%";
                    bton_themhanghoatang.Enabled = false;
                    bton_xoahanghoatang.Enabled = false;
                    tab_khuyenmai.SelectedTabPage = page_2;
                    //label22.Text = "Số tiền tặng";
                    switch (str_lang)
                    {
                        case "vi-VN":
                            label22.Text = "Số tiền tặng";
                            break;
                        case "en-US":
                            label22.Text = "Money give";
                            break;
                    }
                    break;
                    #endregion
                case "11":
                    #region "   Đạt x điểm tặng y tiền"
                    tab_khuyenmai.SelectedTabPage = page_3;
                    //label23.Text = "Số tiền tặng";
                    //label25.Text = "đồng";
                    switch (str_lang)
                    {
                        case "vi-VN":
                            label23.Text = "Số tiền tặng";
                            label25.Text = "đồng";
                            break;
                        case "en-US":
                            label23.Text = "Money give";
                            label25.Text = "VND";
                            break;
                    }
                    radio_tangqua.Enabled = false;
                    radio_tangmathang.Enabled = false;
                    group_tangqua.Enabled = false;
                    bton_themhh_tangdiem.Enabled = false;
                    bton_xoahh_tangdiem.Enabled = false;
                    luoi_tangdiem.Enabled = false;
                    break;
                    #endregion
                case "12":
                    #region "   Đạt x điểm tặng y quà"
                    tab_khuyenmai.SelectedTabPage = page_3;
                    //label23.Text = "Số quà tặng";
                    //label25.Text = "Cái";
                    switch (str_lang)
                    {
                        case "vi-VN":
                            label23.Text = "Số quà tặng";
                            label25.Text = "Cái";
                            break;
                        case "en-US":
                            label23.Text = "Gift give";
                            label25.Text = "thing";
                            break;
                    }
                    radio_tangqua.Enabled = true;
                    radio_tangmathang.Enabled = true;
                    group_tangqua.Enabled = true;
                    bton_themhh_tangdiem.Enabled = true;
                    bton_xoahh_tangdiem.Enabled = true;
                    luoi_tangdiem.Enabled = true;
                    break;
                    #endregion
                case "13":
                    #region "   Miễn phí món - khi đạt doanh thu mức A"
                    //lbltongtien.Text = "Tổng doanh thu";
                    switch (str_lang)
                    {
                        case "vi-VN":
                            lbltongtien.Text = "Tổng doanh thu";
                            label22.Text = "Số lượng tặng";
                            break;
                        case "en-US":
                            lbltongtien.Text = "Total income";
                            label22.Text = "Amount for get";
                            break;
                    }
                    bton_themhanghoatang.Enabled = true;
                    bton_xoahanghoatang.Enabled = true;
                    tab_khuyenmai.SelectedTabPage = page_2;
                   // label22.Text = "Số lượng tặng";
                    lbel_donvitang.Text = lbel_donviban.Text;
                    break;
                    #endregion
                case "14":
                    #region "   Giảm giá - khi đạt doanh thu mức A"
                    //lbltongtien.Text = "Tổng doanh thu";
                    switch (str_lang)
                    {
                        case "vi-VN":
                            lbltongtien.Text = "Tổng doanh thu";
                            
                            break;
                        case "en-US":
                            lbltongtien.Text = "Total income";
                             
                            break;
                    }
                    tab_khuyenmai.SelectedTabPage = page_1;
                    lbel_donvi.Text = "%";
                    #endregion
                    break;
                case "15":
                    #region "   Tặng quà - khi đạt doanh thu mức A"
                    switch (str_lang)
                    {
                        case "vi-VN":
                            lbltongtien.Text = "Tổng doanh thu";

                            break;
                        case "en-US":
                            lbltongtien.Text = "Total income";

                            break;
                    }
                    tab_khuyenmai.SelectedTabPage = page_3;

                    #endregion
                    break;
                case "16"://Giảm % Giảm % thức ăn khách hàng
                case "17"://Giảm % Giảm % thức ăn (thẻ VIP)
                case "18"://Giảm % Giảm % thức uống khách hàng
                case "19"://Giảm % Giảm % thức uống (thẻ VIP)
                    tab_khuyenmai.SelectedTabPage = page_1;
                    lbel_donvi.Text = "%";
                    break;
            }

            switch (com_hinhthuckhuyenmai.SelectedIndex)
            {
                case 1:
                case 3:
                case 5:
                    
                    switch (str_lang)
                    {
                        case "vi-VN":
                            lbel_donvi.Text = "đồng";

                            break;
                        case "en-US":
                            lbel_donvi.Text = "VND";

                            break;
                    }
                    break;
                case 2://
                case 4:
                case 6:
                    lbel_donvi.Text = "%";
                    break;
            }
            //------------------- End hinh thuc khuyen mai moi -----------------------------------
           
        }

        private void bton_tongtiengiam_Click(object sender, EventArgs e)
        {
            string tongtiengiam  = new cpublic().loadformso_point(500, 250);
            if (tongtiengiam != "")
                txt_giatri.Text = tongtiengiam;
        }

        private void bton_soluongban_Click(object sender, EventArgs e)
        {
            string soluongban = new cpublic().loadformso_point(470, 270);
            if (soluongban != "")
                txt_soluongban.Text = soluongban;
        }

        private void bton_soluongtang_Click(object sender, EventArgs e)
        {
            string soluongtang = new cpublic().loadformso_point(470, 270);
            if (soluongtang != "")
                txt_soluongtang.Text = soluongtang;
        }

        #region  "Khuyen mai khong chon khach hang"


        private void bton_themhanghoaban_Click(object sender, EventArgs e)
        {
            //Mod_khuyenmai_hanghoa_diem class_khuyenmai = new Mod_khuyenmai_hanghoa_diem();
            //class_khuyenmai.f_getdata_hanghoaban();

            mh_khuyenmaihanghoa frm_khuyenmaihanghoa = new mh_khuyenmaihanghoa("Hàng hóa");
            frm_khuyenmaihanghoa.ShowDialog();
            tbHH_KM = frm_khuyenmaihanghoa.tbHH_KM;

            luoi_hanghoaban.MainView = grid_hanghoaban;

            if (tbHH_KM != null && tbHH_KM.Columns.Contains("DVT"))
                tbHH_KM.Columns["DVT"].ColumnName = "DVT_BAN";
            if (luoi_hanghoaban.DataSource != null)
            {
                DataTable tb_datasource = ((DataTable)luoi_hanghoaban.DataSource).Copy();
                tb_datasource.PrimaryKey = new DataColumn[] { tb_datasource.Columns["MAHHBAN"] };

                if (tbHH_KM != null)
                {
                    foreach (DataRow dr_tb in tbHH_KM.Rows)
                    {
                        string ma = dr_tb["MAHHBAN"] + "";
                        if (tb_datasource.Rows.Contains(ma) == false)
                        {
                            DataRow dr_datasource = tb_datasource.NewRow();
                            dr_datasource["MAHHBAN"] = dr_tb["MAHHBAN"];
                            dr_datasource["TENHHKM"] = dr_tb["TENHHKM"];
                            dr_datasource["DVT_BAN"] = dr_tb["DVT_BAN"];
                            tb_datasource.Rows.Add(dr_datasource);
                        }

                    }
                    if (tb_datasource.Columns.Contains("STT") == false)
                        tb_datasource.Columns.Add("STT", typeof(int));
                    for (int i = 0; i < tb_datasource.Rows.Count; i++)
                    {
                        tb_datasource.Rows[i]["STT"] = i + 1;
                    }
                    luoi_hanghoaban.DataSource = tb_datasource;

                    tbHH_KM = null;
                }
            }
            else if (tbHH_KM != null)
            {

                luoi_hanghoaban.DataSource = tbHH_KM;
                tbHH_KM = null;
            }
        }

        private void bton_themhanghoatang_Click(object sender, EventArgs e)
        {
            Mod_khuyenmai_hanghoa_diem class_khuyenmai = new Mod_khuyenmai_hanghoa_diem();
            tbHH_KM = class_khuyenmai.f_getdata_hanghoaban();

            luoi_hanghoatang.MainView = grid_hanghoatang;

            if (tbHH_KM != null && tbHH_KM.Columns.Contains("DVT"))
                tbHH_KM.Columns["DVT"].ColumnName = "DVT_TANG";

            if (luoi_hanghoatang.DataSource != null)
            {
                DataTable tb_datasource = ((DataTable)luoi_hanghoatang.DataSource).Copy();
                //tb_datasource.PrimaryKey = new DataColumn[] { tb_datasource.Columns["MAHHBAN"]};

                if (tb_datasource.Columns.Contains("STT") == false)
                {
                    DataColumn colSTT = new DataColumn("STT", typeof(int));
                    tb_datasource.Columns.Add(colSTT);
                }

                if (tbHH_KM != null)
                {
                    foreach (DataRow dr_tb in tbHH_KM.Rows)
                    {
                        string ma = dr_tb["MAHHBAN"] + "";
                        DataRow[] drs = tb_datasource.Select("MAHHTANG='" + ma + "'");
                        //if (tb_datasource.Rows.Contains(ma) == false)
                        if (drs.Length==0)
                        {
                            DataRow dr_datasource = tb_datasource.NewRow();
                            dr_datasource["MAHHTANG"] = dr_tb["MAHHBAN"];
                            //dr_datasource["TENHHTANG"] = dr_tb["TENHHKM"];
                            dr_datasource["TENHHTANG"] = dr_tb["TENHHKM"];
                            dr_datasource["DVT_TANG"] = dr_tb["DVT_TANG"];
                            tb_datasource.Rows.Add(dr_datasource);
                        }

                    }
                    tbHH_KM = null;
                }

                if (tb_datasource != null)
                    if (tb_datasource.Rows.Count > 0)
                        for (int i = 0; i < tb_datasource.Rows.Count; i++)
                        {
                            tb_datasource.Rows[i]["STT"] = i + 1;
                        }
                luoi_hanghoatang.DataSource = tb_datasource;
            }
            else if (tbHH_KM != null)
            {
                tbHH_KM.Columns["TENHHKM"].ColumnName = "TENHHTANG";
                luoi_hanghoatang.DataSource = tbHH_KM;
                tbHH_KM = null;
            }
        }
        // tao so thu tu cho luoi
        private void grid_hanghoaban_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                int stt = 1 + e.RowHandle;
                e.Info.DisplayText = stt.ToString();
               
            }

        }

        private void grid_hanghoatang_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            GridView view = (GridView)sender;
            //Check whether the indicator cell belongs to a data row
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                int stt = 1 + e.RowHandle;
                e.Info.DisplayText = stt.ToString();

            }
        }

        private void bton_xoahanghoaban_Click(object sender, EventArgs e)
        {
            if (grid_hanghoaban.RowCount <= 0)
                return;
            int chiso;
            chiso = ((ColumnView)luoi_hanghoaban.Views[0]).FocusedRowHandle;
            DataView dv_hanghoaban = (DataView)luoi_hanghoaban.Views[0].DataSource;
            DataTable tb = dv_hanghoaban.ToTable();
       
            dv_hanghoaban.Delete(chiso);
            
            //if (grid_hanghoatang.RowCount > chiso)
            //{
            //    DataView dv_hanghoatang = (DataView)luoi_hanghoatang.Views[0].DataSource;
            //    dv_hanghoatang.Delete(chiso);
            //}
            
        }

        private void bton_xoahanghoatang_Click(object sender, EventArgs e)
        {
            if (grid_hanghoatang.RowCount <= 0)
                return;
            int chiso;
            chiso = ((ColumnView)luoi_hanghoatang.Views[0]).FocusedRowHandle;
            DataView dv_hanghoatang = (DataView)luoi_hanghoatang.Views[0].DataSource;
            //if (tb.Columns.Contains("OBJID") && tb.Rows.Count>0)
            //    str_delete_item = tb.Rows[0]["OBJID"] + "";
            dv_hanghoatang.Delete(chiso);
            if (grid_hanghoaban.RowCount > chiso)
            {
                DataView dv_hanghoaban = (DataView)luoi_hanghoaban.Views[0].DataSource;
                dv_hanghoaban.Delete(chiso);
            }
        }

        private void grid_hanghoaban_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (grid_hanghoaban.RowCount <= 0)
                return;
            int chiso;
            chiso = ((ColumnView)luoi_hanghoaban.Views[0]).FocusedRowHandle;
              lbel_donviban.Text = grid_hanghoaban.GetRowCellDisplayText(chiso, col_dvt_hanghoaban) + "";

              if (grid_hanghoatang.RowCount > chiso && (com_hinhthuckhuyenmai._GetselectedID() == "9" || com_hinhthuckhuyenmai._GetselectedID() == "10"))
            {
                grid_hanghoatang.FocusedRowHandle = chiso;
            }
            
            
        }

        private void grid_hanghoatang_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            if (grid_hanghoatang.RowCount <= 0)
                return;
            int chiso;
            chiso = ((ColumnView)luoi_hanghoatang.Views[0]).FocusedRowHandle;
            lbel_donvitang.Text = grid_hanghoatang.GetRowCellDisplayText(chiso, col_dvt_hanghoatang) + "";
            if (grid_hanghoaban.RowCount > chiso)
            {
                grid_hanghoaban.FocusedRowHandle = chiso;
            }
        }
        #endregion 

        #region "khuyen mai doi diem "
        private void bton_themquatang_Click(object sender, EventArgs e)
        {
            int ketqua = -1;
            if (txt_tenquatang.Text != "" && com_dvt._GetselectedID() !="")
            {
                khuyenmaidiem = new Mod_khuyenmai_hanghoa_diem();
                ketqua = khuyenmaidiem.f_insert_quatang(txt_tenquatang.Text, com_dvt._GetselectedID());
                if (ketqua > -1)
                {
                     luoi_quatang.DataSource =  khuyenmaidiem.f_select_khuyenmaiquatang();
                }
                

            }
            

           
        }

        private void bton_xoaquatang_Click(object sender, EventArgs e)
        {
            if (grid_quatang.RowCount <= 0)
                return;

            int ketqua = -1;
            khuyenmaidiem = new Mod_khuyenmai_hanghoa_diem();
            int chiso = ((ColumnView)luoi_quatang.Views[0]).FocusedRowHandle;
            int ma_quatang = int.Parse( grid_quatang.GetRowCellDisplayText(chiso, col_maquatang) + "");
            ketqua = khuyenmaidiem.f_delete_quatang(ma_quatang);
            if (ketqua >-1)
            {
              
                DataView tam = (DataView)luoi_quatang.Views[0].DataSource;
                tam.Delete(chiso);
            }
        }

        #endregion

        private void radio_tangqua_CheckedChanged_1(object sender, EventArgs e)
        {
                        if (radio_tangqua.Checked)
                        {
                            group_tangqua.Enabled = true;
                            //  bton_themhh_tangdiem.Enabled = false;

                        }
                        else
                        {
                            group_tangqua.Enabled = false;
                          //  bton_themhh_tangdiem.Enabled = true;
                        }
        }

        private void bton_themhh_tangdiem_Click(object sender, EventArgs e)
        {
            if (radio_tangqua.Checked)
            {
                if (grid_quatang.RowCount > 0 && grid_tangdiem.RowCount<1)
                {
                    DataTable tb =  new DataTable();
                    tb.Columns.Add("MAHHBAN", typeof(string));
                    tb.Columns.Add("TENQUATANG", typeof(string));
                    tb.Columns.Add("DVT",typeof(string));
                    tb.Rows.Add(grid_quatang.GetRowCellValue(grid_quatang.FocusedRowHandle,col_maquatang),
                                        grid_quatang.GetRowCellValue(grid_quatang.FocusedRowHandle,col_tenquatang),
                                        grid_quatang.GetRowCellValue(grid_quatang.FocusedRowHandle,col_dvtquatang));
                    luoi_tangdiem.DataSource = tb;
                    label25.Text = tb.Rows[0]["DVT"] + "";

                }

            }
            else
            {

                Mod_khuyenmai_hanghoa_diem class_khuyenmai = new Mod_khuyenmai_hanghoa_diem();
                class_khuyenmai.f_getdata_hanghoaban();
                if (tbHH_KM == null)
                    return;
                DataTable tb = new DataTable();
                tb.Columns.Add("MAHHBAN", typeof(string));
                tb.Columns.Add("TENQUATANG", typeof(string));
                tb.Columns.Add("DVT", typeof(string));
                tb.Rows.Add(tbHH_KM.Rows[0]["MAHHBAN"], tbHH_KM.Rows[0]["TENHHKM"], tbHH_KM.Rows[0]["DVT"]);
                                
                luoi_tangdiem.DataSource = tb;
                tbHH_KM = null;
                label25.Text = tb.Rows[0]["DVT"] + "";
            }
        }

        private void bton_xoahh_tangdiem_Click(object sender, EventArgs e)
        {
            if (grid_tangdiem.RowCount > 0)
            {
                int chiso;
                chiso = ((ColumnView)luoi_tangdiem.Views[0]).FocusedRowHandle;
                DataView tam = (DataView)luoi_tangdiem.Views[0].DataSource;
                tam.Delete(chiso);
            
            }
                
        }

        private void txt_soluongtang_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void textEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void grid_hanghoaban_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
              if (e.Column.SortOrder != DevExpress.Data.ColumnSortOrder.None)
            {
                e.Column.SortOrder = DevExpress.Data.ColumnSortOrder.None;
            }
        }

        private void bton_tenquatang_Click(object sender, EventArgs e)
        {
            string tenquatang = new cpublic().loadformchu_point(300, 200);
            if(tenquatang != "")
                txt_tenquatang.Text = tenquatang;
        }

        private void bton_cauhinhdiem_Click(object sender, EventArgs e)
        {
            mh_cauhinhdiem frm_cauhinhdiem = new mh_cauhinhdiem();
            frm_cauhinhdiem.ShowDialog();
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            string _sodiem = new cpublic().loadformso_point(470, 270);
            if (_sodiem != "")
                txt_sodiemdat.Text = _sodiem;
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string _soquatang = new cpublic().loadformso_point(470, 270);
            if (_soquatang != "")
                txt_soquatang.Text = _soquatang;
        }

        private void grid_khuyenmai_Click(object sender, EventArgs e)
        {
            if (grv_khuyenmai.RowCount <= 1)
                this.grid_khuyenmai_FocusedRowChanged(null, null);
        }

        private void btnGhichu_Click(object sender, EventArgs e)
        {
            mh_chu frm_chu = new mh_chu();
            string sghichu = frm_chu.ShowForm(txtGhichu.Text.Trim(), 200, 200, false);
            if (frm_chu.DialogResult == DialogResult.OK)
                txtGhichu.Text = sghichu;
        }

      




     

    
    }
}
